<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;
use think\Db;
/**
 * 理财页面接口控制器
 */
class Template extends ApiBase
{

    public function ccie_data(){
        $mid = input('wallet_id');
        $userid = decoded_user_token(input("user_token"),'member_id');
        $address = Db::name("user_account")->where(["user_id"=>$userid,"wallet_id"=>$mid])->find();
	    if(empty($address)){
            return $this->apiReturn(['code'=>1,'msg'=>'地址不存在']);
        }
        $online_public_info = Db::name("wallet")->where(["symbol"=>"ccie"])->find();
        if(empty($online_public_info)){
            return $this->apiReturn(['code'=>1,'msg'=>'ccie货币不存在']);
        }
        return $this->apiReturn(['code'=>0,'data'=>['ccie'=>$online_public_info,'address'=>$address['blockchain_address2']],'msg'=>'查询成功']);
    }

    public function template_online_data(){
        $templdate_online_data = Db::name("template")->where(["type"=>4,"status"=>1])->find()["content"];
        $templdate_online_data = json_decode($templdate_online_data,true);
        $type  = input('type');
        $page = input('page');
        $limit = input('limit');
        $bet_data = array();
        $data = array();
        foreach($templdate_online_data as $k=>$v){
            if($v["flag"]==$type){
                $data[]=$v;
            }
        }
        $fromat_data = array();
        if($type==1){
            $real_data = Db::name("games_log")
                ->field("a.*,w.blockchain_address2")
                ->alias('a')
                ->join('tp_user_account w ','w.user_id = a.user_id')
                ->where(["a.type"=>4,'is_winner'=>2,'state'=>4,"w.wallet_id"=>6,'entrust_count'=>0])->select();
                 foreach($real_data as $key=>$value){
                     $temp['address']=$value["blockchain_address2"];
                     $temp['amount']=-$value["profit"];
                     $fromat_data[] = $temp;
                }
            $real_data1 = Db::name("investment_bet_log")
                ->field("a.*,w.blockchain_address2")
                ->alias('a')
                ->join('tp_user_account w ','w.user_id = a.user_id')
                ->where(["a.state"=>['gt',1],"a.profit"=>['lt',0],"w.wallet_id"=>6])->select();
            foreach($real_data1 as $key=>$value){
                $temp['address']=$value["blockchain_address2"];
                $temp['amount']=-$value["profit"];
                $fromat_data[] = $temp;
            }
        }else{
                $real_data = Db::name("games_log")
                        ->field("a.*,w.blockchain_address2")
                        ->alias('a')
                        ->join('tp_user_account w ','w.user_id = a.user_id')
                        ->where(["a.type"=>4,'is_winner'=>1,'state'=>4,"w.wallet_id"=>6,'entrust_count'=>0])->select();
                 foreach($real_data as $key=>$value){
                    $temp['address']=$value["blockchain_address2"];
                    $temp['amount']=$value["profit"];
                    $fromat_data[] = $temp;
                 }
                $real_data1 = Db::name("investment_bet_log")
                    ->field("a.*,w.blockchain_address2")
                    ->alias('a')
                    ->join('tp_user_account w ','w.user_id = a.user_id')
                    ->where(["a.state"=>['gt',1],"a.profit"=>['gt',0],"w.wallet_id"=>6])->select();
                foreach($real_data1 as $key=>$value){
                    $temp['address']=$value["blockchain_address2"];
                    $temp['amount']=$value["profit"];
                    $fromat_data[] = $temp;
                }
        }
        

        foreach($data as $k=>$v){
            foreach($v['address'] as $key=>$value){
                $temp['address'] = $value['address'];
                $temp['amount'] = $v['amount'];
                $fromat_data[] = $temp;
            }
        }
        if(($page-1)*$limit>count($fromat_data)){
            return $this->apiReturn(["code"=>0,'msg'=>'无数据']);
        }
        $return  = array_slice($fromat_data,($page-1)*$limit,$limit);
        return $this->apiReturn(["code"=>0,"data"=>$return,'msg'=>'查询成功']);
    }

    //理财页面链上转帐统计接口
    public function online_bet_data(){
        $end_time=strtotime(date("Y-m-d"),time());
        //昨日
        $start_time=strtotime(date("Y-m-d",time()-60*60*24));
        $user_id=decoded_user_token($this->param['user_token'],'member_id');
        // $sys_data = Db::name("template")->where(["type"=>4,"status"=>1])->find()['content'];
        // $sys_data = json_decode($sys_data,true);
        $data['recent_1_days']['bet'] = Db::name("games_daily_log")->where(["create_time"=>["between"=>[$start_time,$end_time]],'user_id'=>$user_id,'type'=>1])->sum("number");
        $data['recent_1_days']['reward'] = Db::name("games_daily_log")->where(["create_time"=>["between"=>[$start_time,$end_time]],'user_id'=>$user_id,'type'=>2])->sum("number");
        //近七天
        $start_time = strtotime(date("Y-m-d",time()-60*60*24*7));
        $data['recent_7_days']['bet'] = Db::name("games_daily_log")->where(["create_time"=>["between"=>[$start_time,$end_time]],'user_id'=>$user_id,'type'=>1])->sum("number");
        $data['recent_7_days']['reward'] = Db::name("games_daily_log")->where(["create_time"=>["between"=>[$start_time,$end_time]],'user_id'=>$user_id,'type'=>2])->sum("number");
        //近30天
        $start_time = strtotime(date("Y-m-d"),time()-60*60*24*30);
        $data['recent_30_days']['bet'] = Db::name("games_daily_log")->where(["create_time"=>["between"=>[$start_time,$end_time]],'user_id'=>$user_id,'type'=>1])->sum("number");
        $data['recent_30_days']['reward'] = Db::name("games_daily_log")->where(["create_time"=>["between"=>[$start_time,$end_time]],'user_id'=>$user_id,'type'=>2])->sum("number");
        //全部
        $data['all'] = Db::name("games_daily_log")->where(["state"=>2])->sum("number");
        return $this->apiReturn(["code"=>0,"data"=>$data,"msg"=>"查询成功"]);
    }
   
}